function Player(/*Img*/sprite, /*vec2*/ position, /*Map*/ mapManager){
	this.sprite = sprite;
	this.position = position;
	this.mapManager = mapManager;
	
	this.facing = 0;
	this.imgIn = 0;
	this.subImg = vec2(this.imgIn, this.facing);
	this.path = null;
	this.target = null;
	
	this.walkSp = 1 / 24;
	this.imgSp = 0;
	this.animation = Animations.Stand;
	this.name = "kram";
}

Player.prototype.moveTo = function(/*vec2*/ position){
	if (this.path != null) return;
	
	this.path = this.mapManager.findPath(this.position, position);
	if (!this.path || this.path.length == 0){ this.path = null; return; }
	
	if (this.path.length > 5){
		this.imgIn = 9;
		this.animation = Animations.Run;
		this.walkSp = 1 / 20;
	}else{
		this.imgIn = 1;
		this.animation = Animations.Walk;
		this.walkSp = 1 / 24;
	}
};

Player.prototype.lookAt = function(/*vec2*/ position){
	var negPos = this.position.clone();
	var facePos = position.clone();
	negPos.mult(-1);
	facePos.sum(negPos);
	
	if (facePos.a ==  0 && facePos.b ==  1){ this.facing = 0; }else
	if (facePos.a ==  1 && facePos.b ==  1){ this.facing = 1; }else
	if (facePos.a ==  1 && facePos.b ==  0){ this.facing = 2; }else
	if (facePos.a ==  1 && facePos.b == -1){ this.facing = 3; }else
	if (facePos.a ==  0 && facePos.b == -1){ this.facing = 4; }else
	if (facePos.a == -1 && facePos.b == -1){ this.facing = 5; }else
	if (facePos.a == -1 && facePos.b ==  0){ this.facing = 6; }else
	if (facePos.a == -1 && facePos.b ==  1){ this.facing = 7; }
};

Player.prototype.move = function(){
	if (this.path == null) return;
	
	if (this.target == null){
		this.target = this.path[0];
		
		this.lookAt(this.target);
		this.path.splice(0,1);
	}else{
		if (this.target.a > this.position.a){
			this.position.a += this.walkSp;
			if (this.position.a > this.target.a) this.position.a = this.target.a;
		}else if (this.target.a < this.position.a){
			this.position.a -= this.walkSp;
			if (this.position.a < this.target.a) this.position.a = this.target.a;
		}
		
		if (this.target.b > this.position.b){
			this.position.b += this.walkSp;
			if (this.position.b > this.target.b) this.position.b = this.target.b;
		}else if (this.target.b < this.position.b){
			this.position.b -= this.walkSp;
			if (this.position.b < this.target.b) this.position.b = this.target.b;
		}
		
		if (this.position.equals(this.target)){
			if (this.path.length == 0){ 
				this.path = null;
				this.animation = Animations.Stand;
			}
			this.target = null;
			this.move();
		}
	}
};

Player.prototype.checkMouse = function(x, y){
	if (this.mapManager.message != null) return;
	
	var mouseP = this.mapManager.game.mouseP;
	var x2 = x + this.sprite.imgW;
	var y2 = y + this.sprite.imgH;
	
	if (mouseP.a > x && mouseP.a < x2 && mouseP.b > y && mouseP.b < y2){
		document.body.style.cursor = "pointer";
		return true;
	}
	
	return false;
};

Player.prototype.step = function(){
	this.move();
};

Player.prototype.draw = function(alpha){
	this.mapManager.setOffset();
	
	var offset = this.sprite.offset;
	var x = this.position.a * config.gridSizeX - this.position.b * config.gridSizeX + this.mapManager.offset.a - offset.a;
	var y = this.position.a * config.gridSizeY + this.position.b * config.gridSizeY + this.mapManager.offset.b - offset.b;
	
	this.subImg.set(Math.floor(this.imgIn), this.facing);
	if (alpha){
		var ctx = this.mapManager.game.eng.ctx;
		ctx.globalAlpha = 0.5;
		this.mapManager.game.eng.drawSprite(this.sprite, this.subImg, vec2(x, y));
		ctx.globalAlpha = 1;
	}else{
		this.mapManager.game.eng.drawSprite(this.sprite, this.subImg, vec2(x, y));
	}
	
	if (this.checkMouse(x, y)){
		this.mapManager.drawName(x + this.sprite.imgW / 2, y, this.name);
	}
};

Player.prototype.loop = function(){
	if (this.animation.imageSpeed > 0){
		this.imgIn += this.animation.imageSpeed;
		if (this.imgIn > this.animation.endIndex) this.imgIn = this.animation.startIndex;
	}else{
		this.imgIn = this.animation.startIndex;
	}
	
	this.step();
	this.draw();
};
